package org.jdns.jfreechess.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelStateHandlerAdapter;

import javax.inject.Inject;

import com.google.inject.Provider;

public class ChannelUnregisteredHandler extends ChannelStateHandlerAdapter {
  private final ChessServer server;
  private final Provider<User> currentUser;

  @Inject
  public ChannelUnregisteredHandler(ChessServer server, Provider<User> currentUser) {
    this.server = server;
    this.currentUser = currentUser;
  }

  @Override
  public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    User user = currentUser.get();

    if (user == null) {
      System.out.println("user quit before logging in");
    } else {
      server.userDisconnected(user);
      System.out.println(user + " disconnected");
    }
  }
}
